//
//  CCYWelcomeViewController.swift
//  03.微博
//
//  Created by 蔡承毅 on 16/9/23.
//  Copyright © 2016年 IOS. All rights reserved.
//

import UIKit
import SDWebImage

class CCYWelcomeViewController: UIViewController {
    
    /// 头像约束
    var iconViewBottomCons: NSLayoutConstraint?
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        // 先显示本地头像
        if let avatar_large = CCYUserAccountVM.shared.userAccount?.avatar_large{
            let url = URL(string: avatar_large)
            self.iconView.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "avatar_default_big"))
        }
        
        // 获取用户信息
        CCYUserAccountVM.shared.loadUserInfo { (error: Error?) in
            if error != nil{
                print("加载用户失败")
                return
            }
           if let avatar_large = CCYUserAccountVM.shared.userAccount?.avatar_large{
              let url = URL(string: avatar_large)
            self.iconView.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "avatar_default_big"))
            }
        }
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    private func setupUI() {
        // 添加子控件
        self.view.addSubview(backgroundImageView)
        self.view.addSubview(iconView)
        self.view.addSubview(messageLabel)
        
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 0))
        
        // 头像
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0))
        iconViewBottomCons = NSLayoutConstraint(item: iconView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: -160)
        self.view.addConstraint(iconViewBottomCons!)
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 85))
        self.view.addConstraint(NSLayoutConstraint(item: iconView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 85))

        // 文字
        self.view .addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0))
        self.view .addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1, constant: 16))
    }
    
    private func startAnimation (){
        iconViewBottomCons?.constant = -(UIScreen.main.bounds.height - 160)
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { 
            self.view.layoutIfNeeded()
            }) { (_) in
//                print("弹簧弹起来")
                
                UIView.animate(withDuration: 0.25, animations: { 
                    self.messageLabel.alpha = 1
                    }, completion: { (_) in
//                        print("整个动画整完了")
                        UIApplication.shared.keyWindow?.rootViewController = CCYTabbarViewController()
                })
        }
        
    }
    
    /// 懒加载
    /// 背景
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))

    /// 头像
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 切圆
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    /// 文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.text = "欢迎归来"
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = UIColor.black
        
        label.alpha = 0
        
        return label
    }()
    
    
}
